package com.carboy.launch.activity.setting;

import android.content.Context;
import android.content.res.Configuration;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.carboy.launch.R;
import com.carboy.launch.activity.column.BaseActivity;
import com.carboy.launch.utils.Tool;
import com.carboy.launch.widgets.WifiHotSetDialog;

/**
 * 热点Activity
 */
public class SHotSpotActivity extends BaseActivity implements OnClickListener {
    private static final String TAG = "SHotSpotActivity";
	// 当前上下文
	private Context mContext;

	// =============== 控件View =================
	private ImageView ashs_break_igview; // 返回键
	private TextView ashs_hstate_tv; // 显示状态
	private ToggleButton hot_switch_togglebtn; // 开关
	// =============== 其他对象 =================
	private WifiManager wifiManager; // wifi连接管理类
	private LinearLayout setting;
	private WifiHotSetDialog wifiHotSetDialog; // wifi连接操作dialog
	private IHotSpotCallback mIHotSpotCallback = new IHotSpotCallback() {
        @Override
        public void saveHotSet() {
            Log.e(TAG, "modify HotSpot information, saveHotSet");
            if(isApEnabled(mContext)) {
                Log.d(TAG, "isApEnabled");
                hot_switch_togglebtn.setChecked(false);
                isOpenWifihot(false);
                hot_switch_togglebtn.postDelayed(new Runnable() {
                    
                    @Override
                    public void run() {
                        hot_switch_togglebtn.setChecked(true);
                        isOpenWifihot(true);
                    }
                }, 2*1000);
            }
        }
    };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_hot_spot);
		mContext = SHotSpotActivity.this;

		initViews();
		initStatusBar();
		initValues();
		initListeners();
	}

	/**
	 * 初始化控件
	 */
	private void initViews() {
		wifiHotSetDialog = new WifiHotSetDialog(mContext, mIHotSpotCallback);
		
	
		ashs_break_igview = (ImageView) this
				.findViewById(R.id.ashs_break_igview); // 返回键
		hot_switch_togglebtn = (ToggleButton) this
				.findViewById(R.id.ashs_switch_togglebtn); // 开关
		
		if (isApEnabled(mContext)) {
			hot_switch_togglebtn.setChecked(true);
			hot_switch_togglebtn.setEnabled(true);
		} else {
			hot_switch_togglebtn.setChecked(false);
			hot_switch_togglebtn.setEnabled(true);
		}
		setting = (LinearLayout) this.findViewById(R.id.ll_hotsetting);
	}

	/**
	 * 初始化参数
	 */
	private void initValues() {
		// 初始化wifi管理类
		wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
	}

	/**
	 * 初始化事件
	 */
	private void initListeners() {
		hot_switch_togglebtn.setOnClickListener(this);
		ashs_break_igview.setOnClickListener(this);
		setting.setOnClickListener(this);
	}

	/**
	 * 点击事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ashs_break_igview: // 返回键
			SHotSpotActivity.this.finish();
			break;
		case R.id.ashs_switch_togglebtn:
			isOpenWifihot(hot_switch_togglebtn.isChecked());
			break;
		case R.id.ll_hotsetting:
			WifiConfiguration apConfighot = wifiManager.getWifiApConfiguration();
			wifiHotSetDialog.showDialog(wifiManager.getWifiApConfiguration());
			
			break;
		}
	}

	/**
	 * 重写返回键
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			SHotSpotActivity.this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	protected void onResume() {
		super.onResume();
		initMsg();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		// Activity被回收,则保存数据
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// 切屏触发
	}

	/**
	 * 初始化显示信息
	 */
	private void initMsg() {
		getHotSpotState();
	}

	private void getHotSpotState() {
		hot_switch_togglebtn.setChecked(isApEnabled(mContext));
	}

	// 判断Wifi热点是否可用：
	public boolean isApEnabled(Context mContext) {
		int state = getWifiApState(mContext);
		return WIFI_AP_STATE_ENABLING == state
				|| WIFI_AP_STATE_ENABLED == state;
	}

	/**
	 * 判断是否打开wifi
	 */
	private void isOpenWifihot(boolean isOpen) {
	    Log.d(TAG, "isOpenWifihot(" + isOpen + ")");
		if (isOpen) { 
			if (!isApEnabled(mContext)) {
				if(wifiManager.isWifiEnabled()){
					wifiManager.setWifiEnabled(false);
				}
				 Tool.setWifiApEnabled(this, wifiManager, true);
			}
		} else { 
			if (isApEnabled(mContext)) {
				 Tool.setWifiApEnabled(this, wifiManager, false);
			}
		}
	}
	
    public interface IHotSpotCallback {
        void saveHotSet();
    }

}
